package _case

import (
	"fmt"
	"sync"
)

type onceMap struct {
	sync.Once
	data map[string]int
}

func (m *onceMap) LoadData() {
	//list := []string{"A", "B", "C", "D", "E", "F"}
	//for _, v := range list {
	//	_, ok := m.data[v]
	//	if !ok {
	//		m.data[v] = 0
	//	}
	//	m.data[v]++
	//}

	m.Do(func() {
		list := []string{"A", "B", "C", "D", "E", "F"}
		for _, v := range list {
			_, ok := m.data[v]
			if !ok {
				m.data[v] = 0
			}
			m.data[v]++
		}
	})
}

func OnceCase() {
	onceMap := &onceMap{
		data: make(map[string]int),
	}
	wg := sync.WaitGroup{}
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			onceMap.LoadData()
		}()
	}
	wg.Wait()
	fmt.Println(onceMap.data)
}
